<template>
  <div class="AddForm" v-loading="loadingFlag">
    <FormPanel ref="FormPanel" :list="formList" :isSubmitBtn="false" />
    <div class="dialogBottmView" v-if="showType != 'show'">
      <el-button @click="closeDialog('refresh')">返 回</el-button>
      <el-button @click="saveDialog"  type="primary">保存</el-button>
    </div>
  </div>
</template>

<script>
/* eslint-disable eqeqeq */
import { getPartTypeBasePage ,savePartTypeBaseInfo} from '@/api/partsmanage/carrierStoreRepair/query';
import { phone,banWord } from '@/utils/validate';
export default {
  name: 'addForm',
  props: {
    initData: {},
    showType: {},
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      falg: 1,
      formList: this.createFormList(),
      userInfo: {},
      FormPanelData: {},
      isShow: false,
      WS_CODE: '',
      WS_NAME: '',
      WS_PERSON: '',
      WS_PHONE: '',
      WS_REMARK: '',
      type: '',
      vcpaList: [],
      isSubmitOk: false,
      loadingFlag: false

    };
  },
  mounted() {
    // eslint-disable-next-line eqeqeq
    if (this.rowData.flag == 2){

      this.formList.forEach(x => {
        if (x.fieldName === 'WS_CODE' || x.fieldName === 'WS_NAME'){
          x.disabled = true
          x.initialValue = this.rowData[x.fieldName]
        }else{
          x.initialValue = this.rowData[x.fieldName]
        }
      })
    }
  },
  methods: {
    async handleInputChange(val, key){
      //这段代码一定要有，清缓存，避免上一次输入影响结果
      if (!val) {
        this.formList.find(x => x.fieldName === 'WS_CODE').initialValue = '';
        this.formList.find(x => x.fieldName === 'WS_NAME').initialValue = '';
        return;
      }
      const tempObj = {};
      if (key === 'WS_CODE'){
        tempObj.wscode = val;
      }
      if (key === 'WS_NAME'){
        tempObj.wsname = val;
      }
      const res = await getPartTypeBasePage(tempObj);
      //异步获取是否存在备件分类
      if (res && res.data){
        this.formList.find(x => x.fieldName === 'WS_CODE').initialValue = '';
        this.formList.find(x => x.fieldName === 'WS_NAME').initialValue = '';
        return this.$message.warning('物流商相应的代码或名称已存在，请重新输入！');
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '物流商代码',
          fieldName: 'WS_CODE',
          // initialValue: this.rowData.roleCode,
          rules: [{required:true, message: '请输入物流商代码',trigger: 'blur' }, {validator: banWord,  trigger: 'blur' }, {max: 10, message: '长度在10个字符以内', trigger: 'blur' }],
          change: val => this.handleInputChange(val, 'WS_CODE')
        },
        {
          type: 'INPUT',
          label: '物流商名称',
          fieldName: 'WS_NAME',
          rules: [{required:true, message: '请输入物流商名称', trigger: 'blur' }, {min:1,max: 50, message: '长度在1-50个字符以内', trigger: 'blur' }],
          change: val => this.handleInputChange(val, 'WS_NAME')
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'WS_PERSON',
          rules: [{max: 8, message: '长度在8个字符以内', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '联系电话',
          fieldName: 'WS_PHONE',
          rules: [{required:true, message: '请输入联系电话',trigger: 'blur' },  {min:1,max: 30, message: '长度在30个字符以内', trigger: 'blur' }],
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          maxlength: 200,
          fieldName: 'WS_REMARK',
          selfCols: 2,
        }
      ];
    },
    // 保存-编辑
    async saveDialog() {
      var formParams = this.$refs.FormPanel.SUBMIT_FORM()
      if (_.isEmpty(formParams.WS_CODE.toString())){
        return this.$message.warning('物流商代码不能为空！');
      }
      if (_.isEmpty(formParams.WS_NAME.toString())){
        return this.$message.warning('物流商名称不能为空！');
      }
      if (_.isEmpty(formParams.WS_PHONE.toString())){
        return this.$message.warning('联系电话不能为空！');
      }
      // if (formParams.WS_PERSON==null){
      //   return this.$message.warning('请不要输入无效联系人！');
      // }
      if (this.rowData.flag == 2){
        formParams.id = this.rowData.ID;
      }
      // 展示加载中动画
      this.loadingFlag = true;
      this.$set(formParams,'recordVersion',this.rowData.RECORD_VERSION)
      const res = await savePartTypeBaseInfo(formParams);
      if (res.resultCode === 200) {
        this.$message({
          type: 'info',
          message: '保存成功'
        });
        // 关闭加载中动画
        this.loadingFlag = false;
        this.closeDialog('refresh');
      }
      // 关闭加载中动画
      this.loadingFlag = false;
    },
    // 返回
    closeDialog() {
      this.$emit('close');
    },
    handleNumber(val, key){
      // 限定只能输入正整数
      // eslint-disable-next-line no-return-assign
      this.$nextTick(() => this.formList.find(x => x.fieldName === key).initialValue = val.replace(/[^0-9]/g, ''));
    }

  },
};
</script>

<style lang="less" scoped>
.clearfix::after {
  content: "";
  height: 0;
  visibility: hidden;
  clear: both;
  display: block;
}
.clearfix {
  zoom: 1;
}
.AddForm {
  height: calc(100vh - 110px);
  overflow: scroll;
  padding-bottom: 60px;
}
</style>
